<template>
  <vxe-modal @close="cancel" :loading="loading" title="选择课题" v-model="dialogVisible" width="60%" showFooter esc-closable show-zoom
             resize>
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="75px">
      <el-form-item label="项目名称:" prop="ktName">
        <el-input
          size="mini"
          v-model="queryParams.ktName"
          placeholder="请输入课题名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目编号:" prop="ktBh">
        <el-input
          size="mini"
          v-model="queryParams.ktBh"
          placeholder="请输入课题名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table height="50vh" ref="table" @row-click="clickRow" highlight-current-row
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
              v-loading="loading" :data="xmsbList">
<!--      <el-table-column :reserve-selection="true" type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center" type="index" width="50"></el-table-column>
      <el-table-column label="年度" sortable align="center" prop="year">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.year" :value="scope.row.year"/>
        </template>
      </el-table-column>
      <el-table-column label="项目名称" sortable align="center" prop="ktName" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleButtonClick(scope.row)"
          >
            <el-link type="primary">{{ scope.row.ktName }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="项目编号" sortable align="center" prop="ktBh"/>
      <el-table-column label="申报单位" sortable align="center" prop="zrBm" show-overflow-tooltip/>
      <el-table-column label="申报资金(万元)" sortable align="center" prop="xmze">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="xmZeButtonClick(scope.row)"
          >
            <el-link type="primary">{{ scope.row.xmze }}</el-link>
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="课题归属" sortable align="center" prop="ktGs">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.kylx_ktgs" :value="scope.row.ktGs"/>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查询项目申报(主表 + 子表) 弹窗 -->
    <xmxq ref="xmxq"></xmxq>

    <!-- 查看项目预算 弹窗 -->
    <XmZe ref="xmze"></XmZe>

    <div slot="footer" class="dialog-footer" style="display: flex; justify-content: center;">
      <el-button icon="el-icon-check" size="mini" type="primary" @click="submitForm">确 定</el-button>
      <el-button icon="el-icon-close" size="mini" type="info" @click="cancel">关 闭</el-button>
    </div>
  </vxe-modal>
</template>

<script>
import {getXmsbList, listXmsb} from "@/api/xmgl/lxgl/xmsb/xmsb";
import Xmxq from "@/views/xmgl/lxgl/xmsb/components/xmxq";
import XmZe from "@/views/xmgl/lxgl/xmsb/components/xmZe";

export default {
  name: "HtXzKt", // 合同编制中 -> 选择课题
  components: {XmZe, Xmxq},
  dicts: ['kylx_fjlx', 'process_business_status', 'kylx_zcry_ktfg', 'year', 'kylx_ssly_cwxt', 'kylx_gqfw', 'kylx_country', 'sys_user_sex', 'kylx_xmfl_cwxt', 'kylx_ssly', 'kylx_hzfs', 'rcpt_zc', 'kylx_yfxmjb', 'kylx_ktgs', 'kylx_ktfl', 'kylx_bz'],
  inject: ['updateXmsbId'],
  data() {
    return {
      dialogVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ktGs: null,
        year: null,
        ktName: null,
        deptId: null,
        userId: this.$store.state.user.id,
      },
      xmsbList: [],
      // 总条数
      total: 0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      selectedRow: null, // 保存当前选中的行
      ktXzList: [],
      xmSbLable: null,
    }
  },
  methods: {
    cancel() {
      this.dialogVisible = false;
    },
    handleOpen() {
      this.dialogVisible = true;
      this.getList()
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 查询项目申报列表
    getList() {
      this.loading = true;
      getXmsbList(this.queryParams).then(response => {
        this.xmsbList = response.rows;
        this.total = response.total;
        setTimeout(() => {
          this.loading = false;
        }, 400)
      });
    },
    // 重置按钮操作
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 查询子表信息
    handleButtonClick(row) {
      this.$refs.xmxq.handleOpen(row);
    },
    // 查询项目总额信息(打开项目总额弹窗)
    xmZeButtonClick(row) {
      this.$refs.xmze.handleOpen(row);
    },
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      this.xmSbLable = this.selectedRow
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 如果点击的行已经被选中，则取消选中
      if (selectedRows.includes(row)) {
        table.toggleRowSelection(row);
      } else {
        // 否则取消之前选中的行，并选中当前点击的行
        selectedRows.forEach(selectedRow => {
          table.toggleRowSelection(selectedRow);
        });
        table.toggleRowSelection(row);
      }
    },
    submitForm() {
      this.updateXmsbId(this.xmSbLable);
      this.dialogVisible = false;
    },
  },
}
</script>

<style scoped>

</style>
